package com.cn.xact.ty_treasured_book.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cn.xact.ty_treasured_book.R;
import com.cn.xact.ty_treasured_book.TYApplicationState;
import com.cn.xact.ty_treasured_book.config.TYConfigBase;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class HomePhoneSelActivity extends Activity {

    String[] mPhones;

    private boolean mIsLaunchMain = false;

    private ViewGroup mMain;
    private ViewGroup mResult;
    private EditText mKeys;
    private TextView mResultsTitle;

    private TYApplicationState mAppStatus;

    private ListView mMyList;
    private PhoneSearchListViewAdapter mMyListViewAdapter;
    private List<Map<String, Object>> listItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

        LayoutInflater inflater = getLayoutInflater();
        mMain = (ViewGroup)inflater.inflate(R.layout.activity_home_phone_sel, null);

        setContentView(mMain);

        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.common_customer_title);

        ImageView tmp = (ImageView)findViewById(R.id.common_title_indicator);
        tmp.setImageResource(R.drawable.arrow_left);
        TextView title1 = (TextView)findViewById(R.id.common_title_first_section);
        title1.setText(R.string.home_user_search_phone);

        tmp.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                HomePhoneSelActivity.this.finish();
            }

        });

        Button beginSearch = (Button)mMain.findViewById(R.id.home_phone_sel_search_button);
        beginSearch.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                showResult();
            }

        });

        mKeys = (EditText)mMain.findViewById(R.id.home_phone_sel_search_key);
        mResultsTitle = (TextView)mMain.findViewById(R.id.home_user_search_phone_result_title);

        mPhones = getIntent().getExtras().getStringArray("phones");
        mIsLaunchMain = getIntent().getExtras().getBoolean("bootmain");
        mAppStatus = new TYApplicationState(this);
    }

    private void showResult() {
        LayoutInflater inflater = getLayoutInflater();
        if (mResult != null)
            mMain.removeView(mResult);

        if (mKeys.getText().toString().equals("")) {
            mResult = (ViewGroup)inflater.inflate(R.layout.activity_home_phone_sel_fail, null);
        } else {
            mResult = (ViewGroup)inflater.inflate(R.layout.activity_home_phone_sel_list, null);
            mMyList = (ListView)mResult.findViewById(R.id.home_user_search_phone_result_list_view);
            listItems = getListItems();
            mMyListViewAdapter = new PhoneSearchListViewAdapter(this, listItems);
            mMyList.setAdapter(mMyListViewAdapter);
        }

        mResultsTitle.setVisibility(View.VISIBLE);
        mMain.addView(mResult, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        setContentView(mMain);
    }

    private void setCurrentPhone(String name) {
        mAppStatus.updateAppStatus(TYConfigBase.TY_APPLICATION_STATUS_CURRENT_PHONE, name);
    }

    private List<Map<String, Object>> getListItems() {
        List<Map<String, Object>> innnerListItems = new ArrayList<Map<String, Object>>();
        for(int i = 0; i < mPhones.length; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("phone", mPhones[i]);
            innnerListItems.add(map);
        }
        return innnerListItems;
    }

    private class PhoneSearchListViewAdapter extends BaseAdapter {

        private List<Map<String, Object>> listItems;
        private LayoutInflater listContainer;

        public final class ListItemView{
            public TextView phone;
     }

        public PhoneSearchListViewAdapter(Context context, List<Map<String, Object>> listItems) {
            listContainer = LayoutInflater.from(context);
            this.listItems = listItems;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return listItems.size();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return listItems.get(arg0);
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ListItemView  listItemView = null;
            if (convertView == null) {
                listItemView = new ListItemView();
                convertView = listContainer.inflate(R.layout.activity_home_phone_sel_list_item, null);

                listItemView.phone = (TextView)convertView.findViewById(R.id.home_user_search_phone_name);
                convertView.setTag(listItemView);
            }else {
                listItemView = (ListItemView)convertView.getTag();
            }

            listItemView.phone.setText((String)listItems.get(position).get("phone"));
            listItemView.phone.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    setCurrentPhone(((TextView)arg0).getText().toString());
                    if (mIsLaunchMain) {
                        Intent tmp = new Intent(HomePhoneSelActivity.this, MainActivity.class);
                        startActivity(tmp);
                        mIsLaunchMain = false;
                    }
                    HomePhoneSelActivity.this.finish();
                }

            });

            return convertView;
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home_phone_sel, menu);
        return true;
    }

}
